import 'package:cloud_medicine/components/message_user_show.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/message_api.dart';
import 'package:cloud_medicine/model/post_message_list.dart';
import 'package:cloud_medicine/pages/post_detail.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class PostMessageListPage extends StatefulWidget {
  @override
  _PostMessageListPageState createState() => _PostMessageListPageState();
}

class _PostMessageListPageState extends State<PostMessageListPage> {
  bool _loading = false;
  List<PostMessageItem> _list = [];

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  // 加载数据
  Future<Null> _loadData() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getPostMessageList_api();
    if (res['code'] == 0) {
      res['data'].forEach((dynamic postMessageItem) {
        PostMessageItem postMessageItemModel =
            PostMessageItem.init(postMessageItem);
        if (mounted) {
          setState(() {
            _list.add(postMessageItemModel);
          });
        }
      });
    } else {
      errorToast(res['message']);
    }
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
  }

  entryPostDetail(int postID) {
    Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => PostDetail(
        postID: postID,
      ),
    ));
  }

  // 移除消息
  _delete(index, id) async {
    setState(() {
      _list.removeAt(index);
    });
    dynamic res = await deleteMessage_api({'id': id, 'type': 2});
    res['code'] == 0
        ? successToast(res['message'])
        : errorToast(res['message']);
  }

  @override
  Widget build(BuildContext context) {
    return _loading
        ? loadingWidget(_loading)
        : ListView.separated(
            itemCount: _list.length,
            separatorBuilder: (BuildContext context, int index) => Divider(
              indent: 70.0,
              height: 1.0,
              color: Colors.grey,
              endIndent: 10.0,
            ),
            itemBuilder: (BuildContext context, int index) {
              return Dismissible(
                background:
                    new Container(color: Theme.of(context).primaryColor),
                key: new Key(_list[index].id.toString()),
                onDismissed: (direction) {
                  _delete(index, _list[index].id);
                },
                child: Material(
                  child: InkWell(
                    onTap: () {
                      entryPostDetail(_list[index].postID);
                    },
                    splashColor: Colors.grey.withOpacity(0.3), // 慢慢展开的颜色
                    highlightColor: Colors.grey.withOpacity(0.1), // 高亮颜色
                    child: Container(
                      height: 60,
                      margin: EdgeInsets.all(10),
                      child: MessageUserShow(
                        avatar: _list[index].author.avatar,
                        realname: _list[index].author.realname,
                        userID: _list[index].author.id,
                        createTime: _list[index].createTime,
                        abstract: _list[index].abstract,
                      ),
                    ),
                  ),
                ),
              );
            },
          );
  }
}
